package liuwanghui.util;

import liuwanghui.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
public class CacheUtil {
    
    // 使用内存Map存储用户信息
    private static final Map<String, String> CACHE = new ConcurrentHashMap<>();
    private static final String USER_ID_KEY = "user:current:id:";
    private static final String USER_ROLE_KEY = "user:current:role:";
    private static final String USER_INFO_KEY = "user:info:";

    /**
     * 设置当前登录用户信息
     */
    public void setCurrentUser(Long userId, String role) {
        String userKey = USER_ID_KEY + userId;
        String roleKey = USER_ROLE_KEY + userId;
        
        CACHE.put(userKey, userId.toString());
        CACHE.put(roleKey, role);
        
        log.info("用户登录信息已缓存, userId: {}, role: {}", userId, role);
    }

    /**
     * 获取当前登录用户ID
     */
    public Long getCurrentUserId() {
        String userIdStr = CACHE.get(USER_ID_KEY);
        if (userIdStr == null) {
            log.warn("未获取到用户ID");
            return null;
        }
        return Long.parseLong(userIdStr);
    }

    /**
     * 获取当前登录用户角色
     */
    public String getCurrentUserRole() {
        String role = CACHE.get(USER_ROLE_KEY);
        if (role == null) {
            log.warn("未获取到用户角色");
        }
        return role;
    }

    /**
     * 清除当前登录用户信息
     */
    public void clearCurrentUser(Long userId) {
        String userKey = USER_ID_KEY + userId;
        String roleKey = USER_ROLE_KEY + userId;
        
        CACHE.remove(userKey);
        CACHE.remove(roleKey);
        
        log.info("用户登录信息已清除, userId: {}", userId);
    }

    /**
     * 缓存用户信息
     */
    public void setUserInfo(Long userId, User user) {
        String key = USER_INFO_KEY + userId;
        String value = JsonUtil.toJson(user);
        CACHE.put(key, value);
        log.info("用户信息已缓存, userId: {}", userId);
    }

    /**
     * 获取缓存的用户信息
     */
    public User getUserInfo(Long userId) {
        String key = USER_INFO_KEY + userId;
        String json = CACHE.get(key);
        if (json == null) {
            log.warn("未获取到用户信息缓存, userId: {}", userId);
            return null;
        }
        return JsonUtil.fromJson(json, User.class);
    }

    /**
     * 删除用户信息缓存
     */
    public void deleteUserInfo(Long userId) {
        String key = USER_INFO_KEY + userId;
        CACHE.remove(key);
        log.info("用户信息缓存已删除, userId: {}", userId);
    }

    /**
     * 根据pattern删除缓存
     */
    public void deleteByPattern(String pattern) {
        try {
            // 遍历所有key，删除匹配的
            CACHE.keySet().removeIf(key -> key.startsWith(pattern));
            log.info("批量删除缓存成功, pattern: {}", pattern);
        } catch (Exception e) {
            log.error("批量删除缓存失败, pattern: {}", pattern, e);
        }
    }

    /**
     * 删除指定key的缓存
     */
    public void delete(String key) {
        try {
            CACHE.remove(key);
            log.info("删除缓存成功, key: {}", key);
        } catch (Exception e) {
            log.error("删除缓存失败, key: {}", key, e);
        }
    }

    /**
     * 设置缓存
     */
    public void set(String key, String value) {
        try {
            CACHE.put(key, value);
            log.info("设置缓存成功, key: {}", key);
        } catch (Exception e) {
            log.error("设置缓存失败, key: {}", key, e);
        }
    }

    /**
     * 获取缓存
     */
    public String get(String key) {
        try {
            return CACHE.get(key);
        } catch (Exception e) {
            log.error("获取缓存失败, key: {}", key, e);
            return null;
        }
    }

    /**
     * 判断key是否存在
     */
    public boolean hasKey(String key) {
        return CACHE.containsKey(key);
    }
} 